using System;
using System.Collections.Generic;
using ManagedWinapi.Windows;

namespace wutils
{
    public class DisallowedWindowException : Exception
    {
    }

    public class WindowRule
    {
        public string ClassName = "*";
        public WindowExStyleFlags? Flags;
        public string Title = "*";

        public bool IsAppliedTo(SystemWindow window)
        {
            return (Title == "*" || Title == window.Title) &&
                   (ClassName == "*" || ClassName == window.ClassName) &&
                   (Flags == null || Flags == (window.ExtendedStyle & Flags));
        }
    }

    public static class WindowRules
    {
        private static readonly List<WindowRule> disallowedRules = new List<WindowRule>();

        private static void InitializeDisallowed()
        {
            //Start menu
            disallowedRules.Add(new WindowRule
                                    {
                                        ClassName = "DV2ControlHost",
                                        Title = "Start Menu",
                                        Flags = WindowExStyleFlags.TOOLWINDOW
                                    });
            //Start button
            disallowedRules.Add(new WindowRule
                                    {
                                        ClassName = "Button",
                                        Title = "Start",
                                        Flags = WindowExStyleFlags.TOOLWINDOW | WindowExStyleFlags.LAYERED
                                    });
            //Start bar
            disallowedRules.Add(new WindowRule
                                    {
                                        ClassName = "Shell_TrayWnd",
                                        Title = "",
                                        Flags = WindowExStyleFlags.TOOLWINDOW
                                    });
            //Start desktop
            disallowedRules.Add(new WindowRule
                                    {
                                        ClassName = "Progman",
                                        Title = "Program Manager",
                                        Flags = WindowExStyleFlags.TOOLWINDOW
                                    });
            //XP Start bar
            disallowedRules.Add(new WindowRule
                                    {
                                        ClassName = "Shell_TrayWnd",
                                        Title = "",
                                        Flags = WindowExStyleFlags.PALETTEWINDOW
                                    });
            //Tool windows
            disallowedRules.Add(new WindowRule
                                    {
                                        Flags = WindowExStyleFlags.TOOLWINDOW
                                    });
            //Palette windows
            disallowedRules.Add(new WindowRule
                                    {
                                        Flags = WindowExStyleFlags.PALETTEWINDOW
                                    });
        }

        public static bool IsAllowed(SystemWindow window)
        {
            if (disallowedRules.Count==0)
                InitializeDisallowed();

            foreach (WindowRule rule in disallowedRules)
            {
                if (rule.IsAppliedTo(window)) return false;
            }
            return true;
        }
    }
}